package com.git.hui.yinshua.web;

import com.git.hui.yinshua.api.model.BizException;
import com.git.hui.yinshua.api.model.ReqInfoContext;
import com.git.hui.yinshua.api.model.ResVo;
import com.git.hui.yinshua.api.model.Status;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author YiHui
 * @date 2024/6/7
 */
@ControllerAdvice
@ResponseBody
@Slf4j
public class ExceptionProcess {

    @ExceptionHandler(Exception.class)
    public ResVo process(Throwable e) {
        log.error("req: {}, exception:", ReqInfoContext.getReqInfo(), e);
        if (e instanceof BizException) {
            return ResVo.errorReturn(Status.newStatus(((BizException) e).getCode(), e.getMessage()));
        }

        return ResVo.error(Status.StatusEnum.UNEXPECT_ERROR, e.getMessage());
    }

}
